package com.beetle123.utils;

import org.apache.commons.lang.time.DateFormatUtils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Date;

/**
 * @author yuanlong.chen
 * @date 2020/11/04
 */
public class LogUtils {

    private static BufferedWriter bufferedWriter;

    public static void println(String str) {
        write(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " " + str);
    }

    private synchronized static void write(String str) {
        if (bufferedWriter == null) {
            String filepath = MyUtils.getBasePath() + "/log.log";
            System.out.println(filepath);
            try {
                bufferedWriter = new BufferedWriter(new FileWriter(filepath, true));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (bufferedWriter != null) {
            try {
                bufferedWriter.write(str + "\n");
                bufferedWriter.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
